**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/inv_sup_placebo_tab.log, text replace
set more off
 
local cutoff = 50
local seed = 9999 
local case_lb = `cutoff'

local covarsmin d_mo_* d_dist_*
local covars vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 d_mo_* d_dist_* tenure
 local heterolist all before after
 
*****************
**BRING IN CASE STATUS + COMPLAINTS DATA AND APPLY DATA RESTRICTION
***************** 
 
do ${rep_root}/code/data_restrict.do
 
*****************
**MERGE WITH OUTCOMES DATA
***************** 
merge 1:1 cr_id acc_id using ${rep_root}/data/outcomes, keep(3) nogen

 
*****************
**DEFINE TREATMENT AND MAKE COVARIATES
***************** 
 

**Drop investigators without enough cases
tab inv_id
sort inv_id 
by inv_id: gen inv_ct = _N
drop if inv_ct<`case_lb'
tab inv_id intake_yr

**Define some covariates
gen po = acc_rank == "POLICE OFFICER"
gen minor_inj = vic_inj_1<=1
gen vic_old = (year(inc_complaint_dt_1) - vic_byr_oldest)>=35 & !missing(vic_byr_oldest)
gen acc_old = acc_age>=35 & !missing(acc_age)
gen cat_3 = acc_cat==3
gen cat_4 = acc_cat==4
 
   
  

forvalues i = 1/25{
	gen d_dist_`i' = inc_district_1==`i'
}
forvalues yr = 2006/2008{
	gen d_yr_`yr' = intake_yr==`yr'

	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
	
}
forvalues yr = 2014/2018{
	gen d_yr_`yr' = intake_yr==`yr'
	
	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
}

gen d_noaff = finding_cd=="NO AFFIDAVIT"
gen d_unfound = finding_cd=="UNFOUNDED"
gen d_nosus = finding_cd=="NOT SUSTAINED"
gen d_exon = finding_cd=="EXONERATED"
gen d_sus = finding_cd=="SUSTAINED"
gen d_addlinv = finding_cd=="ADDITIONAL INVESTIGATION REQUESTED"

 

sort mid
by mid: gen mid_ct = _N
by mid: gen mid_mark = _n==1
tab mid_mark
sum mid_ct if mid_mark==1
tab mid_ct if mid_mark==1
drop mid_ct mid_mark

sort cr_id
by cr_id: gen cr_ct = _N
by cr_id: gen cr_mark = _n==1
tab cr_mark
sum cr_ct if cr_mark==1
tab cr_ct if cr_mark==1
drop cr_ct cr_mark

**Define treatment
 

gen treat_affidavit = finding_cd != "NO AFFIDAVIT" 
gen treat_sustain = finding_cd == "SUSTAINED"   

gen era = inrange(intake_yr, 2006, 2009) 

foreach t in treat_affidavit treat_sustain {

**Create LOM
capture drop resid mo_resid cr_resid mo_cr_resid cr_ct mo_cr_ct inv_tot_resid mo_inv_tot_resid case mo_case inv_ct2 mo_inv_ct2
qui reg `t' `covars'
*qui reg `t' d_mo_*
predict resid, residuals

sort cr_id
by cr_id: egen cr_resid = total(resid)
by cr_id: gen cr_ct = _N

*sort inv_id intake_yr intake_mo
sort inv_id intake_yr
gen case = 1
by inv_id intake_yr: egen inv_tot_resid = total(resid)
by inv_id intake_yr: egen inv_ct2 = total(case)
gen lom_`t' = (inv_tot_resid - cr_resid)/(inv_ct2-cr_ct)

**Just month effects
qui reg `t' d_mo_*
predict mo_resid, residuals

sort cr_id
by cr_id: egen mo_cr_resid = total(mo_resid)
by cr_id: gen mo_cr_ct = _N

*sort inv_id intake_yr intake_mo
sort inv_id intake_yr
gen mo_case = 1
by inv_id intake_yr: egen mo_inv_tot_resid = total(mo_resid)
by inv_id intake_yr: egen mo_inv_ct2 = total(mo_case)
gen mo_lom_`t' = (mo_inv_tot_resid - mo_cr_resid)/(mo_inv_ct2 - mo_cr_ct)

sort inv_id
by inv_id: sum lom_`t'
}
  
   
**************************
**Reduce Form RESULTS
**************************


//INITIATE TABLE TO STORE RESULTS

texdoc init ${rep_root}/output/tables/inv_sup_placebo.tex, replace force
tex \begin{tabular}{l*{10}{c}}
tex \hline\hline
tex                     &\multicolumn{3}{c}{Complaints}&\multicolumn{3}{c}{Arrests}&\multicolumn{3}{c}{Type 1 Arrests} \\
tex \hline
tex                     &\multicolumn{1}{c}{$>$ Month -3}&\multicolumn{1}{c}{Months -12-4}&\multicolumn{1}{c}{Year -2}&\multicolumn{1}{c}{$>$ Month -3}&\multicolumn{1}{c}{Months -12-4}&\multicolumn{1}{c}{Year -2}&\multicolumn{1}{c}{$>$ Month -3}&\multicolumn{1}{c}{Months -12-4}&\multicolumn{1}{c}{Year -2} \\


texdoc close


foreach hetero of local heterolist  {
	
	if "`hetero'" == "all" global rest  
 
	if "`hetero'" == "before" global rest keep if  intake_yearmo<ym(2015,11)
 
	if "`hetero'" == "after" global rest keep if  intake_yearmo>=ym(2015,11)
 
	
preserve
$rest 
local treatvar treat_affidavit

	
local varlist  complaints_prec1_3 complaints_prec4_12 complaints_prey2 arrests_prec1_3 arrests_prec4_12 arrests_prey2 type1_arrests_prec1_3 type1_arrests_prec4_12 type1_arrests_prey2
  foreach var of local varlist{
  
**Create Tables

 
 
	
//REDUCED FORM	
	
 if "`treatvar'" == "treat_affidavit" {
 reg `var' lom_`treatvar' lom_treat_sustain `covars' , cluster(inv_id)
 }
  if "`treatvar'" == "treat_sustain" {
 reg `var' lom_`treatvar' lom_treat_affidavit `covars' , cluster(inv_id)
 }
 
 
 
	matrix v4`var' = e(V)
	local se4`var' = sqrt(v4`var'[1,1])
	
	local beta4`var' = _b[lom_`treatvar']
	boottest lom_`treatvar', weight(webb)  nograph seed(`seed')
	local p4`var' = r(p)
 
	local beta4`var' : di %6.3f `beta4`var''
	local se4`var' : di %6.3f `se4`var''
	local p4`var' : di %6.3f `p4`var'' 
	glo star4_`var' = cond(`p4`var'' <0.01, "***", cond(`p4`var'' <0.05, "**", cond(`p4`var'' <0.1, "*", "")))

scalar N= e(N)
local N`var' = N

 
local se`var' : di %6.3f `se`var''
local N`var' : di %7.0fc `N`var''


sum `var'
local mean_`var' =r(mean)
local mean_`var' : di %6.2f `mean_`var''
}

 
**************************
**OUTPUT OLS RESULTS
**************************
 if "`hetero'" == "before" local varname = "Before McDonald"
if "`hetero'" == "after" local varname = "After McDonald"
if "`hetero'" == "all" local varname = "All"
  
 


texdoc init ${rep_root}/output/tables/inv_sup_placebo.tex, append force  
tex \hline 
tex `varname'  & `beta4complaints_prec1_3'${star4_complaints_prec1_3} & `beta4complaints_prec4_12'${star4_complaints_prec4_12} &`beta4complaints_prey2'${star4_complaints_prey2} & `beta4arrests_prec1_3'${star4_arrests_prec1_3} & `beta4arrests_prec4_12'${star4_arrests_prec4_12} &`beta4arrests_prey2'${star4_arrests_prey2} & `beta4type1_arrests_prec1_3'${star4_type1_arrests_prec1_3} & `beta4type1_arrests_prec4_12'${star4_type1_arrests_prec4_12} &`beta4type1_arrests_prey2'${star4_type1_arrests_prey2}   \\
tex          &(`se4complaints_prec1_3') &(`se4complaints_prec4_12') & (`se4complaints_prey2')& (`se4arrests_prec1_3') &(`se4arrests_prec4_12') & (`se4arrests_prey2') &(`se4type1_arrests_prec1_3') &(`se4type1_arrests_prec4_12') & (`se4type1_arrests_prey2')   \\
tex   &[`p4complaints_prec1_3'] &[`p4complaints_prec4_12']& [`p4complaints_prey2']& [`p4arrests_prec1_3'] &[`p4arrests_prec4_12'] & [`p4arrests_prey2'] &[`p4type1_arrests_prec1_3'] &[`p4type1_arrests_prec4_12'] & [`p4type1_arrests_prey2']   \\
  
  
  tex Dep. var. mean & `mean_complaints_prec1_3'&`mean_complaints_prec4_12'&`mean_complaints_prey2'& `mean_arrests_prec1_3'&`mean_arrests_prec4_12'&`mean_arrests_prey2'& `mean_type1_arrests_prec1_3'&`mean_type1_arrests_prec4_12'&`mean_type1_arrests_prey2'& \\
 tex Observations & `Ncomplaints_prec1_3'&`Ncomplaints_prec4_12'&`Ncomplaints_prey2'& `Narrests_prec1_3'&`Narrests_prec4_12'&`Narrests_prey2'& `Ntype1_arrests_prec1_3'&`Ntype1_arrests_prec4_12'&`Ntype1_arrests_prey2'  \\   
 
 

texdoc close
 
 

restore 
 
}
texdoc init ${rep_root}/output/tables/inv_sup_placebo.tex, append force  
tex \hline\hline
tex \end{tabular}

texdoc close
 
   
*clear
